package com.yang.controller;



import com.yang.annotation.OptLogger;
import com.yang.domain.dto.Result;
import com.yang.domain.dto.UserDTO;
import com.yang.service.IFollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.yang.constants.OptTypeConstants.UPDATE;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2022-10-22
 */
@Api("关注模块")
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Resource
    private IFollowService followService;

    /**
     * 关注或者取消功能
     * @param followUserId 被关注的用户id
     * @param isFollow 是否关注
     * @return 关注或者取关
     */
    @OptLogger(value = UPDATE)
    @PutMapping("/{id}/{isFollow}")
    @ApiOperation(value = "关注或者取消功能")
    public Result follow(@PathVariable("id") Long followUserId, @PathVariable("isFollow") Boolean isFollow) {
         followService.follow(followUserId, isFollow);
         return Result.ok();
    }

    /**
     * 判断当前登录的用户是否关注了
     * @param followUserId 被查询的用户id
     * @return 是否关注
     */
    @GetMapping("/or/not/{id}")
    @ApiOperation(value = "判断当前登录的用户是否关注了")
    public Result isFollow(@PathVariable("id") Long followUserId) {
        boolean isFollow = followService.isFollow(followUserId);
        return Result.ok(isFollow);

    }

    /**共同关注功能
     * 判断当前用户和传递过来的user用户通过redis的set数据结构
     * 去求关注的交集
     * @param followUserId 用户id
     * @return 共同关注的用户id
     */
    @GetMapping("common/{id}")
    @ApiOperation(value = "共同关注功能")
    public Result followCommons(@PathVariable("id") Long followUserId){
        List<UserDTO> userDTos = followService.followCommons(followUserId);
        return Result.ok(userDTos);
    }
}
